package com.sk.seckilldemo.service;

import com.sk.seckilldemo.entity.UserInfo;
import com.sk.seckilldemo.enums.ResultCodeEnum;
import com.sk.seckilldemo.mapper.UserMapper;
import com.sk.seckilldemo.util.Response;
import com.sk.seckilldemo.util.TokenUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * 用户功能实现类
 * */
@Service
@RequiredArgsConstructor
public class UserService {
    private final UserMapper userMapper;

    /**
     * 登录功能
     * */
    public Response validateLogin(UserInfo userInfo){
        UserInfo user = userMapper.validateLogin(userInfo);
        if(user!=null){
            HashMap<String,Object> hs = new HashMap<>();
            String token = TokenUtils.sign(userInfo);

            hs.put("token",token);
            hs.put("user",user);
            return Response.success(ResultCodeEnum.LOGIN_SUCCESS.getCode(),ResultCodeEnum.LOGIN_SUCCESS.getMsg(),hs);
        }else{
            return Response.error(ResultCodeEnum.LOGIN_ERROR.getCode(),ResultCodeEnum.LOGIN_ERROR.getMsg());
        }
    }
}
